﻿using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using MS.InformationManagement.DAL.DocumentCenter;

namespace MS.InformationManagement.ContentHub.ObjectDef.CustomFields
{
    public class DeliveryTraceabilityFieldValue : SPFieldMultiColumnValue
    {
        public DeliveryTraceabilityFieldValue(string value)
            : base(value) { }

        public IEnumerable<DeliveryTraceabilityEntry> Entries {
            get
            {
                XmlSerializer tSerializer = new XmlSerializer(typeof(DeliveryTraceabilityEntry));
                for (int i = 0; i < this.Count; i++)
                {
                    yield return tSerializer.Deserialize(new StringReader(this[i])) as DeliveryTraceabilityEntry;
                }
            }
            set
            {
                int index = 0;
                XmlSerializer tSerializer = new XmlSerializer(typeof(DeliveryTraceabilityEntry));
                StringWriter sWriter = new StringWriter();
                foreach (DeliveryTraceabilityEntry entry in value)
                {
                    tSerializer.Serialize(sWriter, value);
                    this[index] = sWriter.ToString();
                    index++;
                }
            }
        }
    }
}
